推导链
Q1: 需求?→ 红3s → 绿2s → 黄1s → 循环 Q2: 怎么串行?→ async/await 或 Promise 链 Q3: 怎么循环?→ 递归或 while(true)
代码
javascript
const sleep = (ms) => new Promise(r => setTimeout(r,ms))
async function trafiicLight() {
while(true) {
console.log('🔴 红灯')
await sleep(3000)
console.log('🟢 绿灯')
await sleep(2000)
console.log('🟡 黄灯')
await sleep(1000)
}
}